package com.walterlife.javacore;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * SemaphoreTest
 *
 * @author zhuyuwei
 * @date 16/05/2017
 */
public class SemaphoreTest {
//    private static  int count = 10000;
    private static AtomicInteger count = new AtomicInteger(10000);
//    private static volatile int count = 10000;
    public static void main(String[] args) {
        final Semaphore semaphore = new Semaphore(1);
        ExecutorService executor = Executors.newCachedThreadPool();
        for (int i = 0; i < 1000; i++) {
            Runnable task = new Runnable() {
                @Override
                public void run() {
//                    try {
//                        semaphore.acquire();
//                        count -= 1;
//                        semaphore.release();
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    count.decrementAndGet();
//                    count -= 1;
                }
            };
            executor.execute(task);
        }
        System.out.printf("" + count);
        executor.shutdown();
    }
}
